﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using Twi.COMMON.Core;

namespace Twi.DataAccess.Common
{
    /*
    * Copyright: ©2010-2013 Twilight软件开发工作室【Tyingsoft 踏影软件】版权所有
    * Author: xuzhihong
    * Create date: 2013-08-31
    * Website: http://www.tyingsoft.com
    * Description: Twi.DataAccess辅助类
    * 
    */
    public class TwiDAHelper
    {
        /// <summary>
        /// 将 List<DbParameter>中日期参数类型设置为DbType.DateTime
        /// </summary>
        /// <param name="listDbParameters"></param>
        /// <param name="dateTimeFields"></param>
        public static void ListDbParamsForDate(ref IList<DbParameter> listDbParameters, string[] dateTimeFields)
        {
            foreach(string field in dateTimeFields)
            {
                var itm = listDbParameters.FirstOrDefault(c => c.ParameterName == field);
                if(itm != null)
                {
                    itm.DbType = DbType.DateTime;
                }
            }
        }


       /// <summary>
       /// 写底层执行的SQL语句日志
       /// </summary>
       /// <param name="strSql"></param>
       /// <param name="parameters"></param>
        public static void WriteLog(string strSql, IList<DbParameter> parameters = null)
        {
            TwiLogHelper.Debugger("SQL语句："+strSql);
            if(parameters !=null)
            {
                string msg = string.Format("SQL语句参数(共{0}个)：", parameters.Count);
                for (var i = 0; i < parameters.Count; i++) {
                    //msg += string.Format("\r\n\t参数名：{0}",parameters[i].ParameterName);
                    //msg += string.Format("\r\n\t参数值：{0}", parameters[i].Value);
                    //msg += string.Format("\r\n\t参数类型：{0}", parameters[i].DbType.ToString());
                    //if (i > 0 && i < parameters.Count)
                    //{
                    //    msg += "\r\n\t----------";
                    //}
                    //注意：在string.Format中两个{{表示一个{
                    msg += string.Format("\r\n\t{{\"ParameterName:\"{0}\",\"Value\":\"{1}\",\"DbType\":\"{2}\"}}", parameters[i].ParameterName, parameters[i].Value, parameters[i].DbType.ToString());
                    if (parameters.Count - 1 != i) {
                        msg += ",";
                    }
                }
                TwiLogHelper.None(msg);
            }
            else{
              TwiLogHelper.None("SQL语句参数：无。");
            }            
        }

        /// <summary>
        /// 写底层执行的SQL语句日志
        /// </summary>
        /// <param name="command"></param>
        public static void WriteLog(DbCommand command)
        {
            TwiLogHelper.Debugger("SQL语句：" + command.CommandText);
            if (command.Parameters != null)
            {
                string msg = "SQL语句参数：";
                for (var i = 0; i < command.Parameters.Count;i++ )
                {
                    var para = command.Parameters[i];
                    msg += string.Format("\r\n\t参数名：{0}", para.ParameterName);
                    msg += string.Format("\r\n\t参数值：{0}", para.Value);
                    msg += string.Format("\r\n\t参数类型：{0}", para.DbType.ToString());
                    if (i > 0 && i < command.Parameters.Count)
                    {
                        msg += "\r\n\t----------";
                    }
                }
                TwiLogHelper.None(msg);
            }
            else
            {
                TwiLogHelper.None("SQL语句参数：无。");
            } 
        }
    }
}
